<?php
namespace app\common\model;




use think\Db;

class Order extends Base
{

    protected function initialize()
    {
        parent::initialize();

    }

    public function getStatusTextAttr($value,$data)
    {
        $status = [1=>'待确定', 2=>'进行中', 3=>'已完成', 4=>'已评论', 5=>'已取消',
            6=>'申述中', 7=>'已驳回', 8=>'已退款', 9=>'已过期'];
        return $status[$data['status']];
    }

    public function getList($map = []){
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('skill_form f','f.form_id = a.form_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->order('a.update_time')
            ->field('a.*,s.skill_name,f.form_name,u.nick_name')
            ->paginate('',false,['query'=>request()->param()]);
    }

    public function getOrder($map = [])
    {
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('users us','us.user_id = a.to_user','LEFT')
            ->where($map)
            ->order('a.create_time desc')
            ->field('a.*,s.skill_name,u.nick_name,us.nick_name as u_name,u.uuid,us.uuid as sd')
            ->paginate('',false,['query'=>request()->param()]);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取订单评论
     */
    public function getComment($map = [])
    {
        $rows =  $this->alias('a')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('gift g','g.gift_id = a.gift_id','LEFT')
            ->where($map)
            ->field('a.score,a.anonymous,g.gift_name,g.thumbnail,g.img,a.num,a.tags,a.content,a.update_time,u.nick_name,u.header_img,u.user_id')
            ->cache(30)
            ->paginate();
        $items = $rows->items();

        foreach ($items as $k => $item){
            $tags = [];
            if ($item['anonymous']){
                $items[$k]['nick_name'] = getAnonymousName($item['nick_name']);
            }

            $tags['tag']    = ['in',$item['tags']];
            $tags['status'] = 1;
            $items[$k]['tags'] = Db::name('skill_tag')->where($tags)->field('tag_name')->cache(30)->select();
            $items[$k]['user_id'] = hashid($item['user_id']);
            $items[$k]['update_time'] = formatTime($item['update_time'],false);
        }

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$items];
    }


    public static function InterestTag($map = [],$as = false)
    {

        if ($as){
            $field = 's.skill_name as tag';
        }else{
            $field = 's.skill_name';
        }
        return self::alias('a')
            ->where($map)
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->distinct(true)
            ->field($field)
            ->cache(15)
            ->select();
    }

    public function getRows($user_id = 0,$join = [],$map = [])
    {
        $rows =  $this->alias('a')
            ->where($map)
            ->join($join)
            ->field('a.order_id,a.user_id,a.to_user,a.score,a.price,a.status,a.create_time,a.num,f.name as form_name,f.price as form_price,s.skill_name')
            ->cache(3)
            ->order('a.update_time desc')
            ->paginate()->each(function ($item) use ($user_id){
                if($item->user_id == $user_id)
                {
                    $to_user_id = $item->to_user;
                    $item['is_default'] = 1;
                }else{
                    $to_user_id = $item->user_id;
                    $item['is_default'] = 0;
                }

                $item['user_info'] = Users::where('user_id',$to_user_id)->field('nick_name,header_img')->find();
               // $item['user_info']['order_sum'] = Db::table('user_extend')->where('user_id', $to_user_id)->value('order_sum');
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    public function getOne($map = [])
    {
        $join = [
            [ 'skill_apply y','y.apply_id = a.apply_id','LEFT'],
            [ 'skill s','s.skill_id = y.skill_id','LEFT'],
            [ 'skill_form f','f.id = y.skill_form_id','LEFT'],
        ];

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field('a.order_id,a.user_id,a.to_user,a.remark,a.order_time,a.num,a.content,a.score,a.price,a.status,a.create_time,s.skill_name,a.reason,f.name as form_name,f.price as form_price')
//            ->cache(3)
            ->find();
    }

    public function getDetail($map = [])
    {
        $join = [
            [ 'skill_form sf','sf.form_id = a.form_id','LEFT'],
            [ 'skill s','s.skill_id = a.skill_id','LEFT'],
            [ 'gift g','g.gift_id = a.gift_id','LEFT'],
        ];

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field('a.*,g.gift_name,g.img ,g.thumbnail as gift_img,sf.form_name,s.skill_name,s.skill_img,s.spec')
            ->find();
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 评价界面信息
     */
    public function evaluateInfo($map = [])
    {
        $join = [
            [ 'skill s','s.skill_id = a.skill_id','LEFT'],
            [ 'users u','u.user_id = a.user_id','LEFT'],
            [ 'user_extend e','e.user_id = a.user_id','LEFT'],
        ];

        $field = 'a.order_id,s.skill_name,a.order_time,a.num,s.spec,s.tag,u.nick_name,u.header_img,e.noble_id,e.noble_time,a.user_id';

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field($field)
            ->find();
    }

}